package Studys.Dao;

import Studys.Bean.User;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class UserDao {
    static Connection conn = null;
    static PreparedStatement ps = null;
    static ResultSet rs = null;
    public User findUserByNameAndPwd(String name) throws Exception {
        Properties info = new Properties();
        //加载项目下的属性文件，出现异常往外抛，由调用者处理
        FileInputStream fis = new FileInputStream("src/druid.properties");
        //从输入流中加载属性
        info.load(fis);
        //使用德鲁伊连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(info);
        conn=dataSource.getConnection();
        String sql = "select * from student where name = ?";
        ps=conn.prepareStatement(sql);
        //将传参的数据作为通配符来进行查询，若无该参数则创建的user还是为null
        ps.setString(1,name);
        rs=ps.executeQuery();
        User user=null;
        while(rs.next()){
            String id=rs.getString("id");
            String username = rs.getString("name");
            user = new User( id,username);
        }
        return user;
    }
    }

